package com.example.m_kartupay;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.m_kartupay.AccountTransactions.JSONAsyncTask;
import com.example.m_kartupay.library.DatabaseHandler;
import com.example.m_kartupay.library.UserFunctions;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class MainListActivity extends Activity {
	
	TextView account_number;
	TextView balance; 
	TextView point;
	ArrayList<Actors> actorsList;
	
	ActorAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_list);
		account_number = (TextView)findViewById(R.id.tvaccountNo);
		balance = (TextView)findViewById(R.id.tvBalance);
		point = (TextView)findViewById(R.id.tvPoint);
		actorsList = new ArrayList<Actors>();
		Intent inMainMenu = getIntent();
		Bundle b = inMainMenu.getExtras();
		String acc = (String)b.get("account_number");
		String balc = (String)b.get("balance");
		String poi = (String)b.get("points");
		account_number.setText(acc);
		balance.setText(balc);
		point.setText(poi);
		
		new JSONAsyncTask().execute("http://api.kartupay.com/i/transaction-history");
		
		ListView listview = (ListView)findViewById(R.id.list);
		adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);
		
		listview.setAdapter(adapter);
		
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long id) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), actorsList.get(position).getinvoice(), Toast.LENGTH_LONG).show();				
			}
		});
	}


	class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
		
		ProgressDialog dialog;
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(MainListActivity.this);
			dialog.setMessage("Loading, please wait");
			dialog.setTitle("Connecting server");
			dialog.show();
			dialog.setCancelable(false);
		}
		
		@Override
		protected Boolean doInBackground(String... urls) {
			DatabaseHandler db = new DatabaseHandler(getApplicationContext());
			HashMap<String, String> account = db.getUserDetails();		
			String channel_id = "123456789";
			String user_id = (String)account.get("user_id");
			String email = (String)account.get("email");
			String limit = "10";
			String page = "1";
			Log.d("channel_id",channel_id);
			Log.d("user_id",user_id);
			Log.d("email",email);
			Log.d("limit",limit);
			Log.d("page",page);
			UserFunctions userFunction = new UserFunctions();
			JSONObject json = userFunction.transactionHistory(urls[0],channel_id, user_id, "ahalomoan@gmail.com", limit, page);
						
			try {
				
				//------------------>>
				

				String res = json.getString("responseCode");
				Log.d("All Transaction: ", json.toString());
				Log.d("ResponseCode",res);
				

				if (Integer.parseInt(res) ==00) {
									
					JSONObject obj_data = json.getJSONObject("data");	
					
					JSONArray result = obj_data.getJSONArray("results"); 
					
					
					for (int i = 0; i < result.length(); i++) {
						JSONObject object = result.getJSONObject(i);
					  
						Actors actor = new Actors();
						
						actor.setinvoice_no(object.getString("invoice"));
						actor.setorder_no(object.getString("order_no"));
						actor.settype(object.getString("type"));
						actor.setsource(object.getString(" source"));
						actor.setstatus(object.getString("status"));
						actor.setpoints(object.getString("points"));
						actor.setamount(object.getString("amount"));
						actor.settransaction_date(object.getString("transaction_date"));
						//actor.setImage(object.getString("image"));
						
						//Log.d("Image",object.getString("image"));
						
						actorsList.add(actor);
					}
					return true;
				}
				
				//------------------>>
				
			} catch (ParseException e1) {
				e1.printStackTrace();
			
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return false;
		}
		
		protected void onPostExecute(Boolean result) {
			dialog.cancel();
			adapter.notifyDataSetChanged();
			if(result == false)
				Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

		}
	}
	
	

	
	
	
}
